package com.celesea.auth.dept.entity;

import com.celesea.auth.role.entity.RoleCode;
import com.celesea.framework.entity.BusEntity;
import com.celesea.framework.util.Const;
import com.celesea.framework.util.EnvUtil;
import com.celesea.framework.util.StringKit;
import com.celesea.framework.util.Version;
import org.springframework.util.Assert;

import javax.persistence.Column;
import javax.persistence.Table;

@Version
@Table(name="dept")
public class Dept extends BusEntity {

    @Column(name = "`code`",columnDefinition = "编码")
    private String code;
    @Column(name = "`name`",columnDefinition = "名称")
    private String name;
    @Column(name = "`py`",columnDefinition = "拼音")
    private String py;
    @Column(name = "`short_name`",columnDefinition = "简称")
    private String shortName;
    @Column(name = "`sort`",columnDefinition = "排序")
    private Integer sort;
    @Column(name = "`level`",columnDefinition = "级次")
    private Integer level;
    @Column(name = "`parent_id`",columnDefinition = "父级")
    private String parentId;
    @Column(name = "`parent_name`",columnDefinition = "父级名称")
    private String parentName;
    @Column(name = "`description`", columnDefinition = "描述")
    private String description;
    @Column(name = "`company_flag`", columnDefinition = "是否是公司标记:[0：否，1：是]", nullable = false)
    protected Byte companyFlag;
    @Column(name = "`company`", columnDefinition = "所属公司", nullable = false)
    protected String company;
    @Column(name = "`admin_id`", columnDefinition = "管理员", nullable = false)
    protected String adminId;
    @Column(name = "`admin_name`", columnDefinition = "管理员名称", nullable = false)
    protected String adminName;


    public void checkAdmin(){
        boolean isCompany = Const.Bool.isTrue(this.getCompanyFlag());
        boolean reuiredAdminSystem = EnvUtil.hasAnyRoles(RoleCode.ADMIN_SYSTEM);
        if (isCompany) {
            boolean hasAdmin = StringKit.isNotEmpty(this.getAdminId());
            Assert.isTrue(hasAdmin, "公司必须设置管理员");
            Assert.isTrue(reuiredAdminSystem, "您没有权限执行当前操作！");
        }
    }

    public boolean isCurrentRoot() {
        return EnvUtil.getCompany().equals(id);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPy() {
        return py;
    }

    public void setPy(String py) {
        this.py = py;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getParentName() {
        return parentName;
    }

    public void setParentName(String parentName) {
        this.parentName = parentName;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public Byte getCompanyFlag() {
        return companyFlag;
    }

    public void setCompanyFlag(Byte companyFlag) {
        this.companyFlag = companyFlag;
    }

    public String getAdminId() {
        return adminId;
    }

    public void setAdminId(String adminId) {
        this.adminId = adminId;
    }

    public String getAdminName() {
        return adminName;
    }

    public void setAdminName(String adminName) {
        this.adminName = adminName;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

}

